# !/usr/bin/env python
"""
    helpers
    ~~~~~~~


    :Author: swq
    :Date: 2021/07/03 23:29 
"""
import re
from datetime import datetime

import pytz
from pytz import utc


def to_bytes(text, encoding="utf-8"):
    """Transform string to bytes."""
    if isinstance(text, str):
        text = text.encode(encoding)
    return text


def to_unicode(input_bytes, encoding="utf-8"):
    """Decodes input_bytes to text if needed."""
    if not isinstance(input_bytes, str):
        input_bytes = input_bytes.decode(encoding)
    return input_bytes


def slugify(text, delim="-"):
    """Generates an slightly worse ASCII-only slug.
     Taken from the Flask Snippets page.

    :param text: The text will be slugify.
    :param delim: Default "-".
    """
    result = []
    p = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
    for word in p.split(text.lower()):
        if word:
            result.append(word)
    return str(delim.join(result))


def now_utc():
    """Returns a timezone aware utc timestamp."""
    return datetime.now(utc).strftime('%Y%m%d%H')


def now_cn():
    """Returns a timezone aware cn timestamp."""
    cn = pytz.timezone('Asia/Shanghai')
    return datetime.now(cn).strftime('%Y%m%d%H')


if __name__ == '__main__':
    print(slugify('sun_wen'))
    print(now_cn())
    print(now_utc())
